package com.DosonLeung.homeschoolcommunication.Controllers;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

import com.DosonLeung.homeschoolcommunication.App.SystemApplication;
import com.DosonLeung.homeschoolcommunication.Constant.SystemConstant;
import com.DosonLeung.homeschoolcommunication.R;
import com.DosonLeung.homeschoolcommunication.SQLite.MembersDB;
import com.DosonLeung.homeschoolcommunication.Utils.StorageUtil;

import java.util.LinkedList;
import java.util.Map;

/**
 * Created by dosonleung on 12/30/14.
 */
public class GroupChatActivity extends Activity{

    Activity self = this;
    SystemApplication app;

    RelativeLayout groupchatBack;
    RelativeLayout groupchatConfirm;
    ListView groupchatList;
    SimpleAdapter groupchatAdapter;

    LinkedList<Map<String, String>> membersList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.groupchat_activity);
        app = (SystemApplication)getApplication();
        app.clearTargetList();
        initWidgets();
        initList();
    }

    private void initWidgets(){
        groupchatBack = (RelativeLayout) findViewById(R.id.groupchat_back);
        groupchatConfirm = (RelativeLayout) findViewById(R.id.groupchat_confirm);
        groupchatBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pressBack();
            }
        });
        groupchatConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pressConfirm();
            }
        });
    }

    private void initList(){
        int resource = R.layout.groupchat_item;
        String[] from = new String[]{"name", "role"};
        int[] to = new int[]{R.id.groupchat_name,R.id.groupchat_subname};
        insertAllDataList();
        groupchatList = (ListView) findViewById(R.id.groupchat_list);
        groupchatAdapter = new SimpleAdapter(self,membersList,resource,from,to);
        groupchatList.setAdapter(groupchatAdapter);
        groupchatList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CheckBox check = (CheckBox) view.findViewById(R.id.groupchat_check);
                if(check.isChecked()){
                    app.getTargetList().remove(getUidByPosition(i));
                    check.setChecked(false);
                }else{
                    app.getTargetList().add(getUidByPosition(i));
                    check.setChecked(true);
                }
            }
        });
    }

    //往memberList插入数据
    private void insertAllDataList() {
        int size;
        Map<String, String> item;
        LinkedList<Map<String, String>> list;
        MembersDB membersDB = new MembersDB(self, StorageUtil.getChatDir().toString() + "/" + "members.db3", 1);
        list = membersDB.getMembersData();
        membersList = new LinkedList<Map<String, String>>();
        size = list.size();
        for (int i = 0; i < size; i++) {
            item = list.get(i);
            String type = (item.get("type"));
            if (!type.equals("class")) {
                membersList.add(item);
            }
        }
    }

    //点击确定按钮事件
    private void pressConfirm(){
        Intent intent = new Intent(self, ChatActivity.class);
        intent.putExtra("target", "groupchat");//群发信息，uid队列需要请SystemApplication里获取
        intent.putExtra("avatar_path", "groupchat");//群发信息，uid队列需要请SystemApplication里获取
        intent.putExtra("tab", false);
        intent.putExtra("type", false);
        self.startActivityForResult(intent, SystemConstant.CHAT_CODE);
        self.finish();
    }

    private void pressBack(){
        Intent intent = getIntent();
        intent.putExtra("chat", true);
        setResult(SystemConstant.CHAT_CODE, intent);
        self.finish();
    }

    //,在membersList查找
    private String getUidByPosition(int index) {
        String uid = "";
        uid = membersList.get(index).get("uid");
        return uid;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = getIntent();
            Bundle data = new Bundle();
            data.putBoolean("chat", true);
            intent.putExtras(data);
            setResult(SystemConstant.CHAT_CODE, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
